<?php

class MaterielController extends Zend_Controller_Action
{

	public function init()
	{
	$this->view->addHelperPath(
		'ZendX/JQuery/View/Helper', 
		'ZendX_JQuery_View_Helper');
	}

	public function indexAction()
	{
		// action body
	}

	public function addAction()
	{
		$form = new Application_Form_Materiel();
		$envoyer = new Zend_Form_Element_Submit('envoyer');
		$envoyer->setAttrib('id', 'boutonenvoyer');
		$form->addElements(array($envoyer));
		$form->envoyer->setLabel('Ajouter');
		$this->view->form = $form;

		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {
					
				$nom=$form->getValue('nom');
				$type=$form->getValue('type');
				$modele=$form->getValue('modele');
				$idReseau=$form->getValue('idReseau');
				$numeroPoste=$form->getValue('numeroPoste');
				$dateAchat=$form->getValue('dateAchat');
				$prix=$form->getValue('prix');

				$materiels = new Application_Model_DbTable_Materiels();
				$materiels->addMateriel($nom, $type, $modele, $idReseau, $numeroPoste, $dateAchat, $prix);

				$this->_helper->redirector('index');
			} else {
				$form->populate($formData);
			}
		}
	}
	
	
public function updateAction()
	{
		$form = new Application_Form_Materiel();
		$envoyer = new Zend_Form_Element_Submit('envoyer');
		$envoyer->setAttrib('id', 'boutonenvoyer');
		$form->addElements(array($envoyer));
		$form->envoyer->setLabel('Mettre a jour');
		$this->view->form = $form;

		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {
				$id= $form->getValue('id');
				$nom=$form->getValue('nom');
				$type=$form->getValue('type');
				$modele=$form->getValue('modele');
				$idReseau=$form->getValue('idReseau');
				$numeroPoste=$form->getValue('numeroPoste');
				$dateAchat=$form->getValue('dateAchat');
				$prix=$form->getValue('prix');
				$materiels= new Application_Model_DbTable_Materiels();
				$materiels->updateMateriel($id, $nom, $type, $modele, $idReseau, $numeroPoste, $dateAchat, $prix);
				$this->_helper->redirector('list');
				
			} else {
				$form->populate($formData);
			}
		} else {
			$id = $this->_getParam('id', 0);
			if ($id > 0) {
				$materiels = new Application_Model_DbTable_Materiels();
				$form->populate($materiels->getMateriel($id));
			}
		}


	}
	
	public function listAction()
	{
		
		$materiel = new Application_Model_DbTable_Materiels();
		$listMateriel= $materiel->listMateriel();
		$nb=sizeof($listMateriel);
		echo "longueur: ".$nb;


		$this->view->list = $listMateriel;



	}
	
	

}

